<?php
require_once('SQLBase.php');
require_once ('Common.php');
require_once ('UserInfo.php');
/*
 * 获取网页传值
 * getAllSection:获取全部部门基本信息
 * getPageCount 获取数据条数
 * getLevel1Limit 获取部门领导数据分页显示
 * editLevel1GetInfo 编辑时先加载个人数据
 * editLevel1UpdateInfo 编辑时编辑后更新数据
 * deleteLevel1Info 删除等级1某个领导
 * addLevel1GetUserlimit 添加人员之获取人员数据
 * addLevel1GetPageCount 添加level1领导之加获取人员数量
 * editLevel1InsertInfo 添加LEVEL1领导
 * addLevel1Search 搜索要增加的领导
 * 
 * getLevel2Limit 获取分管审批数据分页显示
 * editLevel2GetInfo 获取level某个人的信息
 * Level2IsApproveSaveData 保存LeveL3某个人Isapprove 权限
 * Level2IsFeeSaveData 保存LeveL3某个人IsFee 权限
 * addLevel2GetPageCount 添加level2领导之加获取人员数量
 * addLevel2GetUserlimit 添加人员之获取人员数据
 * deleteLevel2Info 删除等级2某个领导
 * addLevel2GetSearch 搜索要增加的领导
 * 
 * getLevel3Limit  获取分管审批数据
 * deleteLevel3Info 删除某个人的LEVEL3权限
 * addLevel3GetPageCount 添加level3领导之加获取人员数量
 * addLevel3GetUserlimit 添加人员之获取人员数据
 * addLevel3Search 搜索要增加的领导
 */
$data=$_POST;
$model=$data['model'];
$user = new UserInfo();


// print_r($data);
switch ($model){
	case 'getAllSection':$resultArrayAll=getAllSectionData();break;
	case 'getPageCount' :$resultArrayAll=getPageCount();break;
	case 'getLevel1Limit':$resultArrayAll=getLevel1Limit($data['n'],$data['m']);break;
	case 'editLevel1GetInfo':$resultArrayAll=editLevel1GetInfo($data['userId'],$data['sectionName']);break;
	case 'editLevel1UpdateInfo':$resultArrayAll=editLevel1UpdateInfo($data);break;
	case 'deleteLevel1Info':$resultArrayAll=deleteLevel1Info($data['userId'],$data['sectionName']);break;
	case 'addLevel1GetUserlimit':$resultArrayAll=addLevel1GetUserlimit($data['n'],$data['m']);break;
	case 'addLevel1GetPageCount':$resultArrayAll=addLevel1GetPageCount();break;
	case 'addLevel1GetAllSectionInfo' :$resultArrayAll=addLevel1GetAllSectionInfo($data['user_id']);break;
	case 'editLevel1InsertInfo':$resultArrayAll=editLevel1InsertInfo($data);break;
	case 'addLevel1Search':$resultArrayAll=addLevel1Search($data['searchText']);break;
	case 'getLevel2Limit':$resultArrayAll=getLevel2Limit($data['n'],$data['m']);break;
	case 'editLevel2GetInfo':$resultArrayAll=editLevel2GetInfo($data['userId']);break;
	case 'Level2IsApproveSaveData': $resultArrayAll=Level2IsApproveSaveData($data['userId'],$data['sectionId']);break;
	case 'Level2IsFeeSaveData':$resultArrayAll=Level2IsFeeSaveData($data['userId'],$data['sectionId']);break;
	case 'addLevel2GetPageCount':$resultArrayAll=addLevel2GetPageCount();break;
	case 'addLevel2GetUserlimit':$resultArrayAll=addLevel2GetUserlimit($data['n'],$data['m']);break;
	case 'deleteLevel2Info':$resultArrayAll=deleteLevel2Info($data['userId']);break;
	case 'addLevel2Search':$resultArrayAll=addLevel2Search($data['searchText']);break;
	case 'getLevel3Limit':$resultArrayAll=getLevel3Limit();break;
	case 'saveLevel3Leader':$resultArrayAll=saveLevel3Leader($data);break;
	case 'deleteLevel3Info':$resultArrayAll=deleteLevel3Info($data['userId']);break;
	case 'addLevel3GetPageCount':$resultArrayAll=addLevel3GetPageCount();break;
	case 'addLevel3GetUserlimit':$resultArrayAll=addLevel3GetUserlimit($data['n'],$data['m']);break;
	case 'addLevel3Search':$resultArrayAll=addLevel3Search($data['searchText']);break;
    case 'getLeaderPower':
        $r = $user->getApprovePower($data['user_id']);
        if($r == false){
            $resultArrayAll['state'] = 0;
            $resultArrayAll['error'] = 'DB_ERROR';
        }
        else{
            $resultArrayAll['state'] = 1;
            $resultArrayAll['data'] = $r;
        }
}

echo json_encode($resultArrayAll);


/*
 * 获取全部部门基本信息
 */
function getAllSectionData() { // 获取表内容
	$db = new SQLBase ();
	$queryStr = 'SELECT * FROM allsection WHERE section_state=1;';
// 		echo $queryStr;
	$result = $db->queryData ( $queryStr );
	if (! $result||0==$result->num_rows) {
		$result_array ['state'] = - 1; // -1表示数据库出错
	} else {
		$result_array ['state'] = 1;
		while ( !!$temp = $result->fetch_array () ) {
			$result_array ['allsection'][] = array (
					section_ID => $temp ['section_ID'],
					section_name => $temp ['section_name'],
					section_type=>$temp ['section_type']
			);
		}
	}
	return $result_array;
}
/*
 * getPageCount 获取数据条数
 */
function getPageCount(){
	$db=new SQLBase();
	$queryStr='SELECT count(*) FROM leader WHERE power_level=1;';
	$result=$db->queryData($queryStr);
	if(!$result){
		$resultArrayAll['state']=-1;
	}else{
		$temp=$result->fetch_assoc();
		$resultArrayAll['level1PageCounts']=$temp['count(*)'];
		$queryStr='SELECT count(*) FROM leader WHERE power_level=2;';
		$result=$db->queryData($queryStr);
		if(!$result){
			$resultArrayAll['state']=-1;
		}else{
			$temp=$result->fetch_assoc();
			$resultArrayAll['level2PageCounts']=$temp['count(*)'];
			$queryStr='SELECT count(*) FROM leader WHERE power_level=3;';
			$result=$db->queryData($queryStr);
			if(!$result){
				$resultArrayAll['state']=-1;
			}else{
				$temp=$result->fetch_assoc();
				$resultArrayAll['level3PageCounts']=$temp['count(*)'];
				$resultArrayAll['state']=1;
		
			}
		}
	}
	return $resultArrayAll;
}
/*
 * 获取部门领导数据分页显示
 */
function getLevel1Limit($n,$m){
	$db=new SQLBase();
	$queryStr='SELECT leader.user_id,`user`.user_name,allsection.section_name,leader.is_approve,leader.is_fee FROM user,allsection,leader WHERE `user`.user_ID=leader.user_id AND allsection.section_ID=leader.section_id AND leader.power_level=1 LIMIT '.$n.','.$m.';';
// 	echo $queryStr;
	$result=$db->queryData($queryStr);
	if(!$result){
		$resultArrayAll['state']=-1;
	}else{
		$resultArrayAll['state']=1;
		while ($temp=$result->fetch_assoc()){
			$resultArrayAll['data'][]=$temp;
		}
	}
	return $resultArrayAll;
}
/*
 * editLevel1GetInfo 编辑时先加载个人数据
 */
function editLevel1GetInfo($userId,$sectionName){
	$db=new SQLBase();
	$queryStr='SELECT section_ID,section_name FROM allsection WHERE section_state=1 AND section_ID NOT IN(SELECT section_id FROM leader  WHERE user_id='.$userId.' AND section_id NOT IN(SELECT section_ID FROM allsection WHERE section_name="'.$sectionName.'"));';
// 	echo $queryStr;
	$result=$db->queryData($queryStr);
	if(!$result){
		$resultArrayAll['state']=-1;
	}else{
		while ($temp=$result->fetch_assoc()){
		$resultArrayAll['allsection'][]=$temp;
		}
	$queryStr='SELECT leader.user_id,`user`.user_name,allsection.section_name,leader.is_approve,leader.is_fee FROM user,allsection,leader WHERE `user`.user_ID=leader.user_id AND allsection.section_ID=leader.section_id AND leader.power_level=1 AND leader.power_level=1 AND allsection.section_name="'.$sectionName.'" AND leader.user_id='.$userId.';';
// 	echo $queryStr;
	$result=$db->queryData($queryStr);
	if(!$result){
		$resultArrayAll['state']=-1;
	}else{
		$resultArrayAll['state']=1;
		$resultArrayAll['data'][]=$result->fetch_assoc();
	}
	}
	return $resultArrayAll;
}
/*
 * editLevel1UpdateInfo 编辑时编辑后更新数据
 */
function editLevel1UpdateInfo($data) {
	$db = new SQLBase ();
	$queryStr = 'UPDATE leader SET section_id=' . $data ['sectionId'] . ',is_approve=' . $data ['isApprove'] . ',is_fee=' . $data ['isFee'] . ' WHERE user_id=' . $data ['userId'] . ' AND section_id=(SELECT section_ID FROM allsection WHERE section_name="'.$data['sectionName'].'");';
// 	echo $queryStr;
    $result=$db->queryData($queryStr);
	if(!$result){
		$resultArrayAll['state']=-1;
	}else{
		$resultArrayAll['state']=1;
	}
	return $resultArrayAll;
}
/*
 * deleteLevel1Info 删除等级1某个领导
 */
function deleteLevel1Info($userId,$sectionName) {
	$db = new SQLBase ();
	$queryStr = 'DELETE FROM leader WHERE power_level=1 AND user_id=' . $userId . ' AND section_id IN(SELECT section_ID FROM allsection WHERE section_name="'.$sectionName.'");';
// 	echo  $queryStr;
	$result = $db->queryData ( $queryStr );
	if (! $result) {
		$resultArrayAll ['state'] = - 1;
	} else {
		$resultArrayAll ['state'] = 1;
	}
	return $resultArrayAll;
}
/*
 * addLevel1GetUserlimit 添加人员之获取人员数据
 */
function addLevel1GetUserlimit($n,$m){
	$db = new SQLBase ();
	$queryStr='SELECT user_ID,user_name FROM user WHERE user_state=1 LIMIT '.$n.','.$m.';';//AND user_ID NOT IN (SELECT user_id FROM leader WHERE power_level=1) 
// 	echo $queryStr;
	$result=$db->queryData($queryStr);
	if (! $result) {
		$resultArrayAll ['state'] = - 1;
	} else {
		$resultArrayAll ['state'] = 1;
		while($temp=$result->fetch_assoc()){
			$resultArrayAll ['data'][] = $temp;
		}
	}
	return $resultArrayAll;	
}
/*
 * addLevel1GetPageCount 添加level1领导之加获取人员数量
 */
function addLevel1GetPageCount(){
	$db = new SQLBase ();
	$queryStr='SELECT count(*) FROM user WHERE user_state=1  ;';//AND user_ID NOT IN (SELECT user_id FROM leader WHERE power_level=1)
	// 	echo $queryStr;
	$result=$db->queryData($queryStr);
	if (! $result) {
		$resultArrayAll ['state'] = - 1;
	} else {
		$resultArrayAll ['state'] = 1;
		$temp=$result->fetch_assoc();
		$resultArrayAll ['pageCount'] = $temp['count(*)'];

	}
	return $resultArrayAll;
}
/*
 *addLevel1GetPageCount 添加level1领导之加获取人员数量 
 */
function addLevel1GetAllsectionInfo($user_id) {
	$db = new SQLBase ();
	$queryStr = 'SELECT section_ID,section_name FROM allsection WHERE section_state=1 AND section_ID NOT IN(SELECT section_id FROM leader WHERE power_level=1 AND user_id='.$user_id.'); ';//NOT IN(SELECT section_id FROM leader WHERE power_level=1)
	$result = $db->queryData ( $queryStr );
	if (! $result) {
		$resultArrayAll ['state'] = - 1;
	} else {
		$resultArrayAll ['state'] = 1;
		while ( $temp = $result->fetch_assoc () ) {
			$resultArrayAll ['data'] [] = $temp;
		}
	}
	return $resultArrayAll;
}
/*
 * editLevel1InsertInfo 添加LEVEL1领导
 */
function editLevel1InsertInfo($data){
	$db = new SQLBase ();
	$queryStr = 'INSERT INTO leader(user_id,section_id,is_approve,is_fee,power_level) VALUES('.$data['userId'].','.$data['sectionId'].','.$data['isApprove'].','.$data['isFee'].',1);';
// 		echo $queryStr;
	$result=$db->queryData($queryStr);
	if(!$result){
		$resultArrayAll['state']=-1;
	}else{
		$resultArrayAll['state']=1;
	}
	return $resultArrayAll;
}
/*
 * addLevel1Search 搜索要增加的领导
 */
function addLevel1Search($earchText){
	$db = new SQLBase ();
	$queryStr='SELECT user_ID,user_name FROM user WHERE user_state=1 AND user_name LIKE "%%'.$earchText.'%%" AND user_ID NOT IN (SELECT user_id FROM leader WHERE power_level=1);';
// 	echo $queryStr;
	$result=$db->queryData($queryStr);
	if (! $result) {
		$resultArrayAll ['state'] = - 1;
	} else {
		$resultArrayAll ['state'] = 1;
		while($temp=$result->fetch_assoc()){
			$resultArrayAll ['data'][] = $temp;
		}
	}
	return $resultArrayAll;	
}
//**********************************************************************************
/*
 * getLevel2Limit 获取分管审批数据分页显示
 */
function getLevel2Limit($n,$m){
	$db=new SQLBase();
	$queryStr='SELECT DISTINCT leader.user_id,`user`.user_name FROM user,leader WHERE `user`.user_ID=leader.user_id AND leader.power_level=2 LIMIT '.$n.','.$m.';';
	// 	echo $queryStr;
	$result=$db->queryData($queryStr);
	if(!$result){
		$resultArrayAll['state']=-1;
	}else{
		$resultArrayAll['state']=1;
		while ($temp=$result->fetch_assoc()){
			$resultArrayAll['data'][]=$temp;
		}
	}
	return $resultArrayAll;
}
/*
 * editLevel2GetInfo 获取level某个人的信息
 */
function editLevel2GetInfo($userId){
	$db=new SQLBase();
	$queryStr='SELECT section_ID,section_name FROM allsection WHERE section_state=1;';
	$result=$db->queryData($queryStr);
	if(!$result){
		$resultArrayAll['state']=-1;
	}else{
		$resultArrayAll['state']=1;
		while($temp=$result->fetch_assoc()){
			$resultArrayAll['allsection'][]=$temp;
		}
	}
		$queryStr='SELECT section_id FROM leader WHERE is_approve=1 AND user_id='.$userId.';';
		$result=$db->queryData($queryStr);
		if(!$result){
			$resultArrayAll['state']=-1;
		}else{
			$temp=$result->fetch_assoc();
			$resultArrayAll['state']=1;
			$resultArrayAll['isApproveData']=$temp;
			
	    }
	$queryStr='SELECT section_id FROM leader WHERE is_fee=1 AND user_id='.$userId.';';
		$result=$db->queryData($queryStr);
		if(!$result){
			$resultArrayAll['state']=-1;
		}else{
			$resultArrayAll['state']=1;
			$temp=$result->fetch_assoc();
			$resultArrayAll['isFeeData']=$temp;
	    }
	return $resultArrayAll;
}
/*
 * Level2IsApproveSaveData 保存LeveL3某个人Isapprove 权限
 */
function Level2IsApproveSaveData($userId,$sectionId){
	$db=new SQLBase();
	$queryStr='DELETE FROM leader WHERE is_approve=1 AND power_level=2 AND user_id='.$userId.';';
// 	echo $queryStr;
// 	$queryStr='INSERT INTO leader(user_id,section_id,is_approve,is_fee,power_level) VALUES('.$userId.',"'.$sectionId.'",1,0,2);';
// 	echo $queryStr;
	$result=$db->queryData($queryStr);
	if(!$result){
		$resultArrayAll['state']=-1;
	}else{
		if($sectionId!=''){
		$queryStr='INSERT INTO leader(user_id,section_id,is_approve,is_fee,power_level) VALUES('.$userId.',"'.$sectionId.'",1,0,2);';
		$result=$db->queryData($queryStr);
		if(!$result){
			$resultArrayAll['state']=-1;
		}else{
			$resultArrayAll['state']=1;
		}
		}else{
			$resultArrayAll['state']=1;
		}
	}
	return $resultArrayAll;
}
/*
 * Level2IsFeeSaveData 保存LeveL3某个人IsFee 权限
 */
function Level2IsFeeSaveData($userId,$sectionId){
	$db=new SQLBase();
	$queryStr='DELETE FROM leader WHERE is_fee=1 AND power_level=2 AND user_id='.$userId.';';
// 	echo $queryStr;
// 	$queryStr='INSERT INTO leader(user_id,section_id,is_approve,is_fee,power_level) VALUES('.$userId.',"'.$sectionId.'",1,0,2);';
// 	echo $queryStr;
	$result=$db->queryData($queryStr);
	if(!$result){
		$resultArrayAll['state']=-1;
	}else{
		if($sectionId!=''){
		$queryStr='INSERT INTO leader(user_id,section_id,is_approve,is_fee,power_level) VALUES('.$userId.',"'.$sectionId.'",0,1,2);';
		$result=$db->queryData($queryStr);
		if(!$result){
			$resultArrayAll['state']=-1;
		}else{
			$resultArrayAll['state']=1;
		}
		}else{
			$resultArrayAll['state']=1;
		}
	}
	return $resultArrayAll;
}
/*
 * addLevel1GetPageCount 添加level1领导之加获取人员数量
 */
function addLevel2GetPageCount(){
	$db = new SQLBase ();
	$queryStr='SELECT count(*) FROM user WHERE user_state=1 AND user_ID NOT IN (SELECT DISTINCT user_id FROM leader WHERE power_level=2) ;';
	// 	echo $queryStr;
	$result=$db->queryData($queryStr);
	if (! $result) {
		$resultArrayAll ['state'] = - 1;
	} else {
		$resultArrayAll ['state'] = 1;
		$temp=$result->fetch_assoc();
		$resultArrayAll ['pageCount'] = $temp['count(*)'];

	}
	return $resultArrayAll;
}
/*
 * addLevel2GetUserlimit 添加人员之获取人员数据
 */
function addLevel2GetUserlimit($n,$m){
	$db = new SQLBase ();
	$queryStr='SELECT user_ID,user_name FROM user WHERE user_state=1 AND user_ID NOT IN (SELECT DISTINCT user_id FROM leader WHERE power_level=2) LIMIT '.$n.','.$m.';';
	// 	echo $queryStr;
	$result=$db->queryData($queryStr);
	if (! $result) {
		$resultArrayAll ['state'] = - 1;
	} else {
		$resultArrayAll ['state'] = 1;
		while($temp=$result->fetch_assoc()){
			$resultArrayAll ['data'][] = $temp;
		}
	}
	return $resultArrayAll;
}
/*
 * deleteLevel2Info 删除等级2某个领导
 */
function deleteLevel2Info($userId) {
	$db = new SQLBase ();
	$queryStr = 'DELETE FROM leader WHERE power_level=2 AND user_id=' . $userId . ';';
	$result = $db->queryData ( $queryStr );
	if (! $result) {
		$resultArrayAll ['state'] = - 1;
	} else {
		$resultArrayAll ['state'] = 1;
	}
	return $resultArrayAll;
}
/*
 * addLevel2Search 搜索要增加的领导
 */
function addLevel2Search($earchText){
	$db = new SQLBase ();
	$queryStr='SELECT user_ID,user_name FROM user WHERE user_state=1 AND user_name LIKE "%%'.$earchText.'%%" AND user_ID NOT IN (SELECT user_id FROM leader WHERE power_level=2);';
	// 	echo $queryStr;
	$result=$db->queryData($queryStr);
	if (! $result) {
		$resultArrayAll ['state'] = - 1;
	} else {
		$resultArrayAll ['state'] = 1;
		while($temp=$result->fetch_assoc()){
			$resultArrayAll ['data'][] = $temp;
		}
	}
	return $resultArrayAll;
}
/*
 * getLevel3Limit 获取分管审批数据分页显示
 */
function getLevel3Limit(){
	$db=new SQLBase();
	$queryStr='SELECT DISTINCT leader.user_id,`user`.user_name,leader.is_approve,leader.is_fee FROM user,leader WHERE `user`.user_ID=leader.user_id AND leader.power_level=3;';
	// 	echo $queryStr;
	$result=$db->queryData($queryStr);
	if(!$result){
		$resultArrayAll['state']=-1;
	}else{
		$resultArrayAll['state']=1;
		while ($temp=$result->fetch_assoc()){
			$resultArrayAll['data'][]=$temp;
		}
	}
	return $resultArrayAll;
}
/*
 * saveLevel3Leader 保存level3的领导信息
 */
function saveLevel3Leader($data){
	$db=new SQLBase();
	$queryStr='DELETE FROM leader WHERE power_level=3 AND user_id='.$data['userId'].';';
// 	echo $queryStr;
    $result=$db->queryData($queryStr);
    if(!$result){
    	$resultArrayAll['state']=-1;
    }else{
    	$queryStr=$queryStr='INSERT INTO leader(user_id,section_id,is_approve,is_fee,power_level) VALUES('.$data['userId'].',"all",'.$data['isApprove'].','.$data['isFee'].',3);';
    	$result=$db->queryData($queryStr);
    	if($result){
    		$resultArrayAll['state']=1;
    	}
    }
    return $resultArrayAll;
}
/*
 * deleteLevel3Info 删除等级2某个领导
 */
function deleteLevel3Info($userId) {
	$db = new SQLBase ();
	$queryStr = 'DELETE FROM leader WHERE power_level=3 AND user_id=' . $userId . ';';
	$result = $db->queryData ( $queryStr );
	if (! $result) {
		$resultArrayAll ['state'] = - 1;
	} else {
		$resultArrayAll ['state'] = 1;
	}
	return $resultArrayAll;
}
/*
 * addLevel1GetPageCount 添加level1领导之加获取人员数量
 */
function addLevel3GetPageCount(){
	$db = new SQLBase ();
	$queryStr='SELECT count(*) FROM user WHERE user_state=1 AND user_ID NOT IN (SELECT DISTINCT user_id FROM leader WHERE power_level=3) ;';
	// 	echo $queryStr;
	$result=$db->queryData($queryStr);
	if (! $result) {
		$resultArrayAll ['state'] = - 1;
	} else {
		$resultArrayAll ['state'] = 1;
		$temp=$result->fetch_assoc();
		$resultArrayAll ['pageCount'] = $temp['count(*)'];

	}
	return $resultArrayAll;
}
/*
 * addLevel2GetUserlimit 添加人员之获取人员数据
 */
function addLevel3GetUserlimit($n,$m){
	$db = new SQLBase ();
	$queryStr='SELECT user_ID,user_name FROM user WHERE user_state=1 AND user_ID NOT IN (SELECT DISTINCT user_id FROM leader WHERE power_level=3) LIMIT '.$n.','.$m.';';
	// 	echo $queryStr;
	$result=$db->queryData($queryStr);
	if (! $result) {
		$resultArrayAll ['state'] = - 1;
	} else {
		$resultArrayAll ['state'] = 1;
		while($temp=$result->fetch_assoc()){
			$resultArrayAll ['data'][] = $temp;
		}
	}
	return $resultArrayAll;
}
/*
 * addLevel3Search 搜索要增加的领导
 */
function addLevel3Search($earchText){
	$db = new SQLBase ();
	$queryStr='SELECT user_ID,user_name FROM user WHERE user_state=1 AND user_name LIKE "%%'.$earchText.'%%" AND user_ID NOT IN (SELECT user_id FROM leader WHERE power_level=3);';
	// 	echo $queryStr;
	$result=$db->queryData($queryStr);
	if (! $result) {
		$resultArrayAll ['state'] = - 1;
	} else {
		$resultArrayAll ['state'] = 1;
		while($temp=$result->fetch_assoc()){
			$resultArrayAll ['data'][] = $temp;
		}
	}
	return $resultArrayAll;
}
?>